// pages/identity/identity.js
var that,
  api = new (require('../../utils/api.js')),
  headimg = api.getimgurl(),
  app = getApp(),
  util = new (require('../../utils/util.js'));
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isselecet: !1,
    islogin: !0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this
    let user = app.logininfo.user;
    if (user.nickName && user.avatarUrl) {
      that.setData({ islogin: !1 })
    }
    that.setData({ user })
    that.getdata()
  },
  getdata() {
    api.identity(res => {
      if (res.data.issuccess == 1) {
        let identitydata = res.data.data
        that.setData({ identitydata })
        console.log(res.data.data, 'identity');
      }
    })
  },
  getphone(e) {
    let phone = e.detail.value
    that.setData({ phone })
  },
  toselecet() {
    let { isselecet } = that.data
    that.setData({ isselecet: !isselecet })
  },
  modsend() {
    that.setData({ isselecet: !1 })
  },
  // getuserinfo(e) {
  //   let { islogin } = that.data
  //   if (!islogin) {
  //     if (e.detail.userInfo) {
  //       let { userInfo } = e.detail;
  //       api.getnameurl({ avatarUrl: userInfo.avatarUrl, nickName: userInfo.nickName }, res => {
  //         if (res.data.issuccess == 1) {
  //           that.setData({ islogin: !1 }, () => {
  //             app.logininfo.user.avatarUrl = userInfo.avatarUrl
  //             app.logininfo.user.nickName = userInfo.nickName
  //           })
  //           that.submit()
  //         } else {
  //           wx.showModal({
  //             title: '提示',
  //             content: '登录失败，请重新登录',
  //             success: function (res) {
  //               if (res.confirm) {
  //                 that.getuserinfo(e)
  //               }
  //             },
  //           })
  //         }
  //       })
  //     }
  //   }
  // },


  getUserProfile() {
    let { islogin } = that.data
    if (!islogin) {
      wx.getUserProfile({
        desc: '用于完善用户资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (res) => {
          console.log(res.userInfo, 'resres');
          let userinfo = res.userInfo
          if (res.userInfo) {
            api.getnameurl({ avatarUrl: userInfo.avatarUrl, nickName: userInfo.nickName }, res => {
              if (res.data.issuccess == 1) {
                that.setData({ islogin: !1 }, () => {
                  app.logininfo.user.avatarUrl = userInfo.avatarUrl
                  app.logininfo.user.nickName = userInfo.nickName
                })
                that.submit()
              } else {
                wx.showModal({
                  title: '提示',
                  content: '登录失败，请重新登录',
                  success: function (res) {
                    if (res.confirm) {
                      that.getUserProfile()
                    }
                  },
                })
              }
            })
          } else {
            console.log("点击了拒绝授权");

          }
        },
        fail(res) {
          console.log("点击了拒绝授权");

        },
      })


      //       if (e.detail.userInfo){
      //         let {userInfo} = e.detail;
      //         api.getnameurl({avatarUrl:userInfo.avatarUrl,nickName:userInfo.nickName},res=>{
      //           if(res.data.issuccess==1){
      //             that.setData({islogin:!1},()=>{
      //                 app.logininfo.user.avatarUrl=userInfo.avatarUrl
      //                 app.logininfo.user.nickName=userInfo.nickName
      //             })
      //            that.submit()
      //           }else{
      //             wx.showModal({
      //               title: '提示',
      //               content:'登录失败，请重新登录',
      //               success:function(res){
      //                 if(res.confirm){
      //                   that.getuserinfo(e)
      //                 }
      //               },
      //             })
      //           }
      //         })
      //       }
    }





  },
  submit() {
    let { phone } = that.data;
    if (phone.length != 11) {
      wx.showModal({
        title: '请输入正确的手机号',
        content: err,
      })
      return;
    }
    api.addidentity(phone, res => {
      if (res.data.issuccess == 1) {
        console.log(res, 'ressss');
        that.getdata()
        that.setData({ isselecet: !1 })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})